package com.dbq.fast.business.main.dialog;


import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.DialogFragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;

import com.dbq.fast.R;

import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;

/**
 * Created by sunkai on 2017/11/3.
 */

public class NewUserRewardDialog extends DialogFragment {

    private static final String DIALOG_TAG = "new_user_reward_dialog";
    private View.OnClickListener onClickListener;

    private Unbinder unbinder;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        getDialog().requestWindowFeature(Window.FEATURE_NO_TITLE);
        getDialog().getWindow().setBackgroundDrawable(new ColorDrawable(0x00ffffff));
        View view = LayoutInflater.from(getActivity()).inflate(R.layout.dialog_new_user_reward, container, true);
        unbinder = ButterKnife.bind(this, view);
        return view;
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }

    public static NewUserRewardDialog newInstance() {
        return new NewUserRewardDialog();
    }

    @OnClick({R.id.close})
    public void onClick(View view) {
        if (onClickListener != null) {
            onClickListener.onClick(view);
        }
        dismiss();
    }

    public static NewUserRewardDialog show(FragmentManager fragmentManager) {
        FragmentTransaction ft = fragmentManager.beginTransaction();
        if (fragmentManager.findFragmentByTag(DIALOG_TAG) instanceof NewUserRewardDialog) {
            NewUserRewardDialog prev = (NewUserRewardDialog) fragmentManager.findFragmentByTag(DIALOG_TAG);
            if (prev != null) {
                if (prev.isVisible()) {
                    prev.dismiss();
                }
                ft.remove(prev);
            }
            ft.addToBackStack(null);
        }
        NewUserRewardDialog newUserRewardDialog = NewUserRewardDialog.newInstance();
        try {
            newUserRewardDialog.show(ft, DIALOG_TAG);
        }catch (Exception e){}
        return newUserRewardDialog;

    }

    public void setOnClickListener(View.OnClickListener onClickListener) {
        this.onClickListener = onClickListener;
    }
}
